<?php

include dirname(dirname(dirname(__FILE__))) . '/include/common.php';

class webServiceUpdateEvent extends core {

    public function process() {
        $data = array();
        $event_id = intval($_POST['event_id']);
        $user_id = intval($_POST['user_id']);
        if ($_POST['event_name'] === "") {
            $this->_JSONResponse(array('error' => 1, 'msg' => "Please enter event's name."));
        }
        if ($_POST['location'] === "") {
            $this->_JSONResponse(array('error' => 1, 'msg' => "Please enter event's location."));
        }
        if ($_POST['start_time'] === "" || $_POST['start_time'] == "Start Time*") {
            $this->_JSONResponse(array('error' => 1, 'msg' => "Please enter event's start time."));
        }
        $current_date = date('Y-m-d', time());
        if (strtotime($_POST['event_date']) < strtotime($current_date)) {
            $this->_JSONResponse(array('error' => 1, 'msg' => 'Start date cannot be sooner than current date!'));
        }
        if (strtotime($_POST['event_date'] . " " . $_POST['start_time']) < time()) {
            $this->_JSONResponse(array('error' => 1, 'msg' => 'Start time cannot be sooner than current time!'));
        }
        if ($_POST['end_time'] != '') {
            if (strtotime($_POST['event_date'] . " " . $_POST['start_time']) > strtotime($_POST['event_date'] . " " . $_POST['end_time'])) {
                $this->_JSONResponse(array('error' => 1, 'msg' => 'End time cannot be sooner than start time!'));
            }
        }
        $data['title'] = strip_tags(trim($_POST['event_name']));
        $start_date = date('Y-m-d H:i:s', strtotime($_POST['event_date']));
        $data['start_date'] = $start_date;

        $start_time = date('Y-m-d H:i:s', strtotime($_POST['event_date'] . " " . $_POST['start_time']));
        $data['start_time'] = $start_time;
        $end_time = date('Y-m-d H:i:s', strtotime($_POST['event_date'] . " " . $_POST['end_time']));
        $data['end_time'] = $end_time;
        $data['location'] = strip_tags(trim($_POST['location']));
        $data['description'] = strip_tags(trim($_POST['description']));
        $data['user_id'] = $user_id;
        $data['timestamp'] = date('Y-m-d H:i:s', time());

        $fields = array();
        $values = array();
        $this->_preDataForInsert($data, $fields, $values);
        $fields = implode(',', $fields);
        $values = implode(',', $values);

        if ($event_id > 0) {
            $event_id = $this->db->escape($event_id);
            $result_event = $this->db->get_row("SELECT * FROM events WHERE event_id=$event_id AND user_id=$user_id");

            if ($result_event) {

                $title = $this->db->escape($data['title']);
                $start_date = $data['start_date'];
                $start_time = $data['start_time'];
                $end_time = $data['end_time'];
                $location = $this->db->escape($data['location']);
                $description = $this->db->escape($data['description']);
                $query = "UPDATE `events` SET `title`='$title',`description`='$description',`start_date`='$start_date',`start_time`='$start_time',`end_time`= '$end_time',`location`='$location',
                        `timestamp`='" . $data['timestamp'] . "' WHERE `event_id`= '$event_id'";

                if ($this->db->query($query) === false) {

                    $this->_JSONResponse(array('error' => 1, 'msg' => 'Oops, something went wrong. Please try again'));
                } else {
                    $this->_JSONResponse(array('error' => 0, 'msg' => 'Changes have been saved'));
                }
            } else {
                $this->_JSONResponse(array('error' => 1, 'msg' => 'The Event does not exist'));
            }
        } else {
            if ($this->db->query("INSERT INTO `events` ({$fields}) VALUES ({$values});") === false) {

                $this->_JSONResponse(array('error' => 1, 'msg' => 'Oops, something went wrong. Please try again'));
            } else {
                $this->_JSONResponse(array('error' => 0, 'msg' => 'Event has been added'));
            }
        }
    }

}

$processor = new webServiceUpdateEvent();
$processor->process();
?>